package com.salton123.whiteboard;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * User: wujinsheng1@yy.com
 * Date: 2020/4/29 19:37
 * ModifyTime: 19:37
 * Description:
 */
public class BoardView extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mSurfaceHolder;
    private static final String TAG = "BoardView";

    public BoardView(Context context) {
        super(context, null);
    }

    public BoardView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BoardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        //init surfaceHolder
        mSurfaceHolder = this.getHolder();
        mSurfaceHolder.addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
//        Canvas lockCanvas = getLockCanvas();
//        if (lockCanvas != null) {
//            if (mBackgroundInfo != null) {
//                if (mBackgroundInfo.backgroundType == BackgroundInfo.TYPE_COLOR) {
//                    lockCanvas.drawBitmap(mBackgroundInfo.backgroundBitmap, 0, 0, null);
//                } else {
//                    if (mBackgroundInfo.backgroundColor == -1) {
//                        lockCanvas.drawColor(WHITE_BOARD_DEFAULT_COLOR);
//                    } else {
//                        lockCanvas.drawColor(mBackgroundInfo.backgroundColor);
//                    }
//                }
//            } else {
//                lockCanvas.drawColor(WHITE_BOARD_DEFAULT_COLOR);
//            }
//        }
        drawBackground();
//        this.renderThread = new RenderThread();
//        this.renderThread.setPriority(Thread.MAX_PRIORITY);
//        this.renderThread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
//        this.renderThread.done();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_CANCEL) {
            return false;
        }
        return super.onTouchEvent(event);

    }

    /**
     * 绘制背景
     */
    private void drawBackground() {
        Canvas canvas = mSurfaceHolder.lockCanvas();
        if (canvas == null) {
            return;
        }
        if (mBackgroundInfo != null) {
            if (mBackgroundInfo.backgroundType == BackgroundInfo.TYPE_COLOR) {
                canvas.drawBitmap(mBackgroundInfo.backgroundBitmap, 0, 0, null);
            } else {
                if (mBackgroundInfo.backgroundColor == -1) {
                    canvas.drawColor(WHITE_BOARD_DEFAULT_COLOR);
                } else {
                    canvas.drawColor(mBackgroundInfo.backgroundColor);
                }
            }
        } else {
            canvas.drawColor(WHITE_BOARD_DEFAULT_COLOR);
        }

        if (type == 1) {
            //设置Path
            SvgPath path = new SvgPath();
            path.moveTo(100, 100);
            path.lineTo(300, 100);
            path.lineTo(200, 300);
            path.moveTo(200, 300);
            path.quadTo(400, 50, 600, 300);
            path.addRect(100, 500, 500, 900, Path.Direction.CW);
            Paint paint = new Paint();
            paint.setColor(Color.BLACK);
            paint.setStyle(Paint.Style.FILL);
            path.draw(canvas, paint);
        }
        mSurfaceHolder.unlockCanvasAndPost(canvas);
    }

    private int type = 1;

    public void setBackgroundInfo(BackgroundInfo mBackgroundInfo) {
        this.mBackgroundInfo = mBackgroundInfo;
    }

    private static final int WHITE_BOARD_DEFAULT_COLOR = Color.parseColor("#FFFFFF");
    private BackgroundInfo mBackgroundInfo;
    private RenderThread renderThread;

    private final Object lock = new Object();

    private class RenderThread extends Thread {
        private volatile boolean done;

        @Override
        public void run() {
            Log.i(TAG, "start render...");
            while (!done) {
                synchronized (lock) {
                    render();
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        public void done() {
            this.done = true;
            try {
                notifyRender();
                join();
                Log.i(TAG, "done...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private Canvas getLockCanvas() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            return this.mSurfaceHolder.lockHardwareCanvas();
        } else {
            return this.mSurfaceHolder.lockCanvas();
        }
    }

    public void render() {
//        Canvas lockCanvas;
//        try {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//                lockCanvas = this.mHolder.lockHardwareCanvas();
//            } else {
//                lockCanvas = this.mHolder.lockCanvas();
//            }
//            if (lockCanvas != null) {
//                renderBackgroundColor(lockCanvas);
//                if (renderMode == RENDER_MODE_THUMBNAIL) {
//                    renderThumbnail(lockCanvas);
//                } else {
//                    doFrame(lockCanvas);
//                }
//            }
//            XLog.i(TAG, "render cost :" + (SystemClock.elapsedRealtime() - renderStartTime) + "ms");
//        } catch (Throwable throwable) {
//            XLog.e(TAG, throwable.toString());
//        } finally {
//            if (lockCanvas != null) {
//                this.mHolder.unlockCanvasAndPost(lockCanvas);
//            }
//        }
//
//        if (type == 1) {
//            //设置Path
//            SvgPath path = new SvgPath();
//            path.moveTo(100, 100);
//            path.lineTo(300, 100);
//            path.lineTo(200, 300);
//            path.moveTo(200, 300);
//            path.quadTo(400, 50, 600, 300);
//            path.addRect(100, 500, 500, 900, Path.Direction.CW);
//            Paint paint = new Paint();
//            paint.setColor(Color.BLACK);
//            paint.setStyle(Paint.Style.FILL);
//            path.draw(canvas, paint);
//        }
    }

    /**
     * 通知线程去渲染
     */
    public void notifyRender() {
        synchronized (lock) {
            lock.notifyAll();
        }
    }
}
